package com.ohuang.crashhandler;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;



import java.util.List;

class MyAdapter extends BaseAdapter {
    private Context context;
    private List<String> data;
    private TextOnClick textOnClick;

    public MyAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;
    }


    public TextOnClick getTextOnClick() {
        return textOnClick;
    }

    public void setTextOnClick(TextOnClick textOnClick) {
        this.textOnClick = textOnClick;
    }



    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_crash_log, viewGroup, false);
        TextView textView = inflate.findViewById(R.id.tv_logName);
        textView.setText(data.get(position));
        if (textOnClick != null) {
           textView.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            textOnClick.onClick(position, data.get(position));
                        }
                    }
            );
           textView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    textOnClick.onLongClick(v,position,data.get(position));
                    return true;
                }
            });
        }
        return inflate;
    }

    private static class TextHolder  {
        TextView textView;

        public TextHolder(View itemView) {

            textView = itemView.findViewById(R.id.tv_logName);
        }
    }

    public interface TextOnClick {
        void onClick(int p, String s);

        void onLongClick(View view,int p,String s);
    }
}
